import React , { PureComponent } from 'react';
import { connect } from 'dva';
import { Button, Icon, Card ,Form} from 'antd';
import Result from '../../components/Result';
import PageHeaderLayout from '../../layouts/PageHeaderLayout';

@connect(state => ({
  radioStation: state.radioStation,
}))
@Form.create()
export default class WeiXinSuccess extends PureComponent{
  state = {
    rsId:null,
  };

  componentDidMount() {
    var rsId = this.localQuery(this,"rsId");
    this.setState({
      rsId:rsId
    });
  }

  getQueryString = (name) => {
    let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    let r = window.location.search.substr(1).match(reg);
    if (r !== null) return unescape(r[2]);
    return null;
  };

  localQuery = (_that, name) => {
    let value = '';
    if (!this.isEmpty(_that) &&
      !this.isEmpty(_that.props) &&
      !this.isEmpty(_that.props.location) &&
      !this.isEmpty(_that.props.location.query)) {
      value = _that.props.location.query[name];
    }
    if (this.isEmpty(value)) {
      value = this.getQueryString(name);
    }
    return value;
  };

  isNull = (obj) =>  {
    return obj === null || typeof obj === 'undefined' || obj === undefined;
  };

  /**
   * 判断是不是空的字符串
   * @param obj
   * @returns {boolean}
   */

  isEmpty = (obj)  => {
    return this.isNull(obj) || obj === '';
  };

  turnToRadioStationAddPage(){
    const { dispatch} = this.props;
    const rsId = this.state.rsId;
    if(rsId != null) {
      const param = {
        rsId:rsId,
        showDetail:true
      }
      dispatch({
        type: 'radioStation/turnToRadioStationAddPage',
        payload:param
      });
    }else{
      dispatch({
        type: 'radioStation/turnToRadioStationAddPage'
      });
    }
  }

  turnToRadioStationListPage(){
    const { dispatch} = this.props;
    dispatch({
      type: 'radioStation/turnToRadioStationListPage'
    });
  }

  render() {
    const actions = (
      <div>
        <Button type="primary" onClick={() => this.turnToRadioStationListPage() }>返回列表</Button>
        <Button onClick={() => this.turnToRadioStationAddPage() }>查看当前电台</Button>
      </div>
    );
    return (
      <PageHeaderLayout>
        <Card bordered={false}>
          <Result
            type="success"
            title="绑定成功"
            description="恭喜您，微信公众号已绑定成功"
            actions={actions}
            style={{ marginTop: 48, marginBottom: 16 }}
          />
        </Card>
      </PageHeaderLayout>
    );
  }
};
